package com.nintendo.tool;

import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.data.MutableDataSet;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class EmailTool {
    private final JavaMailSender mailSender;
    private final String from;
    @Autowired
    public EmailTool(JavaMailSender mailSender, @Value("${spring.mail.username}") String from) {
        this.mailSender = mailSender;
        this.from = from;
    }
    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    public static class EmailRequest {
        @ToolParam(description = "收件人邮箱")
        public String email;
        @ToolParam(description = "发送邮件的标题")
        public String subject;
        @ToolParam(description = "发送邮件的内容")
        public String message;
        @ToolParam(description = "邮件内容类型是否为html还是markdown,如果是markdown则为1,如果是html则为2")
        private Integer contentType;
    }

    @Tool(description = "给指定邮箱发送信息, email 为收件人邮箱, subject 为邮件标题, message 为邮件内容")
    public String sendEmail(EmailRequest emailRequest) {
        log.info("==================sendEmail=================");
        log.info("================参数 email: {}=================",emailRequest.email);
        log.info("================参数 subject: {}=================",emailRequest.subject);
        log.info("================参数 message: {}=================",emailRequest.message);
        Integer contentType = emailRequest.getContentType();
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        try {
            helper.setFrom( from);
            helper.setTo(emailRequest.email);
            helper.setSubject(emailRequest.subject);
            if(contentType == 1){
                helper.setText(convertToHtml(emailRequest.getMessage()),true);

            }else if(contentType == 2){
                helper.setText(emailRequest.getMessage(),true);
            }else{
                helper.setText(emailRequest.getMessage());
            }
            mailSender.send(mimeMessage);
            return "发送邮件成功";
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return "发送失败";
    }
    public static String convertToHtml(String markdownStr){
        MutableDataSet dataSet = new MutableDataSet();
        Parser parser = Parser.builder(dataSet).build();
        HtmlRenderer htmlRenderer = HtmlRenderer.builder(dataSet).build();
        return htmlRenderer.render(parser.parse(markdownStr));
    }
    @Tool(description = "查询我的邮箱/邮箱地址")
    public String getMyEmailAddress() {
        log.info("==================getMyEmailAddress=================");
        return "759709135@qq.com";
    }
}
